<?php
namespace Tongcheng\Controller;
use Think\Controller;
class NewsController extends CommonController {
    public function index(){
    	if(IS_AJAX){
            $model = M("News");
            $map['wid'] = session('wid');
            $res['count'] = $model->where($map)->count();
            $page = $_GET['page']?$_GET['page']:1;
            $limit = $_GET['limit']?$_GET['limit']:10;
            $res['list'] = $model->where($map)->order("last_time desc")->page("{$page},{$limit}")->select();
            foreach($res['list'] as $key=>$value){
                $sql = "select * from tp_tui where wid=".session('wid')." and aid='".$value['id']."' and (type='news' or type ='ad')";
                $tui = M()->query($sql);

                if($tui[0]['status'] == 1){
                    $res['list'][$key]['is_tui'] = 1;
                }else{
                    $res['list'][$key]['is_tui'] = 0;
                }
                if(empty($value['cate_id'])){
                    $res['list'][$key]['cate_name'] = "无";
                }else{
                    $cate_name = M('news_cate')
                        ->where(array('id'=>$value['cate_id']))
                        ->getField('name');
                    $res['list'][$key]['cate_name'] = $cate_name;
                }

            }
            //var_dump($res);exit;
            $this->ajaxReturn($res);
        }else{
            $this->display();
        }
    }

    //商品搜索
    public function search()
    {
        header("Content-type: text/html; charset=utf-8");
        $keyword = trim($_GET['keyword']);//关键词
        $eq['wid'] = session('wid');
        $eq['title'] = ['like',"%$keyword%"];
        $list = M('goods')->where($eq)->select();
        $this->ajaxReturn($list);
    }

    public function add(){
        if(IS_AJAX){
            $model = M("News");
            $new['wid'] = session('wid');
            $new['title'] = I('post.title');
            $new['auther'] = I('post.auther');
            $new['pics'] = I('post.pics');
            $new['video_url'] = I('post.video_url');
            $new['content'] = $_POST['content'];
            $new['create_time'] = date("Y-m-d H:i:s");
            $new['cate_id'] = I('post.cate_id');
            $new['type'] = I('type');//类型0文章 1广告
            $new['position'] = I('position');//1 上 2 下
            $new['goodsid'] = I('goodsid')?I('goodsid'):0;
            if(empty($new['title'])){
                $this->error('请输入标题！');
            }
            if(empty($new['auther'])){
                $this->error('请输入作者！');
            }
            if(empty($new['video_url']) && empty($new['content'])){
                $this->error('请输入视频链接或资讯内容！');
            }
            if($model->add($new)){
                $this->success('添加成功！');
            }else{
                $this->error('网络忙，请稍后再试！');
            }
        }else{
            $cateList = M('news_cate')
                ->where(array('wid'=>session('wid'),'status'=>1))
                ->select();
            $this->assign('news_cate',$cateList);
            $this->display();
        }
    }

    public function edit(){
        $model = M("News");
        $map['id'] = $_GET['id'];
        if(IS_AJAX){
            $new['title'] = I('post.title');
            $new['auther'] = I('post.auther');
            $new['pics'] = I('post.pics');
            $new['video_url'] = I('post.video_url');
            $new['content'] = $_POST['content'];
            $new['cate_id'] = I('post.cate_id');
            $new['type'] = I('type');//类型0文章 1广告
            $new['position'] = I('position');//1 上 2 下
            if(I('goodsid')){
                $new['goodsid'] = I('goodsid');
            }
            if(empty($new['title'])){
                $this->error('请输入标题！');
            }
            if(empty($new['auther'])){
                $this->error('请输入作者！');
            }
            if(empty($new['video_url']) && empty($new['content'])){
                $this->error('请输入视频链接或资讯内容！');
            }
            $model->where($map)->save($new);
            $this->success('修改成功！');
        }else{
            $vo = $model->where($map)->find();
            if($vo['goodsid']){
                $goods = M('goods')->where(['id'=>$vo['goodsid']])->find();
                $vo['goodsid'] = $goods['id'];
                $vo['gtitle'] = $goods['title'];
            }
            $pics = explode(",", $vo['pics']);
            $vo['pics_arr'] = "";
            foreach ($pics as $k => $v) {
                $vo['pics_arr'] .= "'{$v}',";
            }
            $cateList = M('news_cate')
                ->where(array('wid'=>session('wid'),'status'=>1))
                ->select();
            $this->assign('news_cate',$cateList);
            $this->assign('vo',$vo);
            $this->display('add');
        }
    }

    public function top(){
        $model = M("News");
        $map['wid'] = session('wid');
        $map['id'] = $_POST['id'];

        $new['is_top'] = $_POST['status'];
        if($model->where($map)->save($new)){
            $this->success("置顶成功！");
        }else{
            $this->error("网络忙，请稍后再试！".$model->getLastSql());
        }
    }

    public function tui(){
        $model = M("News");
        $type = I('type');
        $map['wid'] = session('wid');
        $map['id'] = $_POST['id'];
        $new['is_tui'] = $_POST['status'];
        $news = $model->where($map)->find();
        $new['last_time']  = $news['last_time'];
        if($type == 0){
            $r = tui("news",$_POST['id'],session('wid'),$_POST['status']);
        }else{
            $r = tui("ad",$_POST['id'],session('wid'),$_POST['status']);
        }
        if($model->where($map)->save($new) && $r){
            $this->success("推荐成功！");
        }else{
            $this->error("网络忙，请稍后再试！");
        }
    }

    public function del(){
        $model = M("News");
        $map['wid'] = session('wid');
        $map['id'] = $_POST['id'];

        if($model->where($map)->delete()){
            $this->success("删除成功！");
        }else{
            $this->error("网络忙，请稍后再试！");
        }
    }
    public function cate(){
        if(IS_AJAX){
            $model = M("news_cate");
            $map['wid'] = session('wid');
            $res['count'] = $model->where($map)->count();
            $page = $_GET['page']?$_GET['page']:1;
            $limit = $_GET['limit']?$_GET['limit']:10;
            $res['list'] = $model->where($map)->order("add_time desc")->page("{$page},{$limit}")->select();
            foreach($res['list'] as $key=>$value){
                $time = date("Y-m-d H:i:s",$value['add_time']);
                $res['list'][$key]['add_time'] = $time;
            }
            $this->ajaxReturn($res);
        }else{
            $this->display();
        }
    }
    public function cateAdd(){
        if(IS_AJAX){
            $model = M("news_cate");
            $new['wid'] = session('wid');
            $new['name'] = I('post.name');
            $new['status'] = I('post.status');
            $new['add_time'] = time();

            if(empty($new['name'])){
                $this->error('请输入分类名称！');
            }
            if($model->add($new)){
                $this->success('添加成功！');
            }else{
                $this->error('网络忙，请稍后再试！');
            }
        }else{
            $this->display();
        }
    }
    public function cateEdit(){
        $model = M("news_cate");
        $map['id'] = $_GET['id'];

        if(IS_AJAX){
            $new['name'] = I('post.name');
            $new['status'] = I('post.status');

            if(empty($new['name'])){
                $this->error('请输入标题！');
            }
            $model->where($map)->save($new);
            $this->success('修改成功！');
        }else{
            $vo = $model->where($map)->find();

            $this->assign('vo',$vo);
            $this->display('cateAdd');
        }
    }
    public function cateDel(){
        $model = M("news_cate");
        $map['wid'] = session('wid');
        $map['id'] = $_POST['id'];

        if($model->where($map)->delete()){
            $this->success("删除成功！");
        }else{
            $this->error("网络忙，请稍后再试！");
        }
    }

}